<?php

namespace App\Http\Controllers\Application;

use App\Http\Controllers\Controller;
use App\Http\Requests\Application\CollistDataRequest;
use App\Models\Application\CollistData;
use App\Models\Application\Muticolumn;
use App\Models\Application\Pagetpl;
use App\Models\Application\Paneltpl;
use App\Models\Entity\Asset\Video\Vasseti;
use App\Models\Entity\Asset\Video\Vseriali;
use App\Models\Entity\Column\Collist;
use App\Models\Entity\Column\ColumnType;
use App\Models\Entity\Tag\tag;
use App\Models\Recommend\ApppageRectpl;
use App\Models\Recommend\Reclist0;
use App\Repositories\Application\CollistDataRepository;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Flash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\Rule;
use Response;
use Validator;

class CollistDataController extends Controller
{
    use ModelForm;

    /** @var  CollistDataRepository */
    private $collistDataRepository;

    public function __construct(CollistDataRepository $collistDataRepo)
    {
        $this->collistDataRepository = $collistDataRepo;
    }

    /**
     * Display a listing of the CollistData.
     *
     * @return Response
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(__('application.collist_datas.index_header'));
            $content->description(__('application.collist_datas.index_description'));

            $content->body($this->grid());
        });
    }

    /**
     * Show the form for creating a new CollistData.
     *
     * @return Response
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(__('application.collist_datas.create_header'));
            $content->description(__('application.collist_datas.create_description'));

            $content->body($this->form());

        });
    }

    /**
     * Store a newly created CollistData in storage.
     *
     * @param CollistDataRequest $request
     *
     * @return Response
     */
    public function store(Request $request)
    {
        $input = $request->all();

        // 单个页面只能对应单个布局
        $image = $request->file('icon'); //使用laravel 自带的request类来获取一下文件.
        if ($image) {
            if ($image->isValid()) {
                // 允许上传的图片格式
                $allowed_extensions = ["png", "jpg", "gif", "jpeg"];
                if ($image->getClientOriginalExtension() && !in_array($image->getClientOriginalExtension(), $allowed_extensions)) {
                    return ['error' => 'You may only upload png, jpg, jpeg or gif.'];
                }
                $realPath = $image->getRealPath(); //临时文件的绝对路径
                // 获得图片的扩展名
                $extension = $image->getClientOriginalExtension();
                // 获取上传图片的文件名
                // $imagesName = $image->getClientOriginalName();
                // 重新命名上传文件名字
                $newImageName = md5(time()) . random_int(5, 5) . "." . $extension;
                // 上传图片
                $uploadConfig = config('custom.base.upload.collist_data');
                $driver       = $uploadConfig['driver'];
                $dir          = $uploadConfig['dir'];
                $rootDir      = config('filesystems.disks.' . $driver . '.upload');
                // 图片的完整路径
                $fullPath = $rootDir . '/' . $dir . '/' . $newImageName;

                $input['icon'] = $fullPath;

                $bool = Storage::disk($driver)->put($fullPath, file_get_contents($realPath));
            }
        }

        // 背景图片
        $backgroundImage = $request->file('bg'); //使用laravel 自带的request类来获取一下文件.
        if ($backgroundImage) {
            if ($backgroundImage->isValid()) {
                // 允许上传的图片格式
                $allowed_extensions = ["png", "jpg", "gif", "jpeg"];
                if ($backgroundImage->getClientOriginalExtension() && !in_array($backgroundImage->getClientOriginalExtension(), $allowed_extensions)) {
                    return ['error' => 'You may only upload png, jpg, jpeg or gif.'];
                }
                $bgRealPath = $backgroundImage->getRealPath(); //临时文件的绝对路径
                // 获得图片的扩展名
                $bgExtension = $backgroundImage->getClientOriginalExtension();
                // 获取上传图片的文件名
                // $imagesName = $image->getClientOriginalName();
                // 重新命名上传文件名字
                $newBackgroundImageName = md5(time()) . random_int(5, 5) . "." . $bgExtension;
                // 上传图片
                $uploadConfig = config('custom.base.upload.collist_data');
                $driver       = $uploadConfig['driver'];
                $dir          = $uploadConfig['dir'];
                $rootDir      = config('filesystems.disks.' . $driver . '.upload');
                // 图片的完整路径
                $bgFullPath  = $rootDir . '/' . $dir . '/' . $newBackgroundImageName;
                $input['bg'] = $bgFullPath;
                $bool        = Storage::disk($driver)->put($bgFullPath, file_get_contents($bgRealPath));
            }
        }

        // 验证规则
        $sValidators = [
            'collist_id' => 'bail|required',
            'data_type'  => 'required',
            // 'data_id'    => 'required|unique:collist_datas',
            'data_id'    => [
                'required',
                Rule::unique('collist_datas')
                    ->where(function ($query) use ($input) {
                        $query->where(['collist_id' => $input['collist_id'], 'data_type' => $input['data_type']])->whereNull('deleted_at');
                    }),
            ],
        ];

        $validators = [];

        // 动态判断校验
        foreach ($input as $key => $value) {
            if (isset($sValidators[$key])) {
                $validators[$key] = $sValidators[$key];
            }
        }

        // 定义校验, 没有移到request
        $validator = Validator::make($request->all(), $validators, [
            'required' => ':attribute 不能为空',
            'unique'   => ':attribute 已在此栏目列表中存在',
        ], [
            'collist_id' => '栏目列表',
            'data_type'  => '名称',
            'data_id'    => '数据',
        ]);

        if ($validator) {
            // 校验
            if ($validator->fails()) {
                return back()->withErrors($validator)->withInput();
            }
        }

        $collistData = $this->collistDataRepository->create($input);
        if ($collistData) {
            // 保存当前栏目列表的标签
            if (is_array($request->get('tags'))) {
                $collistData->tags()->attach(array_filter($request->get('tags')));
            }
        }
        Flash::success(__('application.collist_datas.saved_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('application.collist_datas.index'));

    }

    /**
     * Display the specified CollistData.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $collistData = $this->collistDataRepository->findWithoutFail($id);

        if (empty($collistData)) {
            Flash::error(__('application.collist_datas.not_found'));

            return redirect(route('application.collist_datas.index'));
        }

        return view('application.collist_datas.show')->with('collistData', $collistData);

    }

    /**
     * Show the form for editing the specified CollistData.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $collistData = $this->collistDataRepository->findWithoutFail($id);

        if (empty($collistData)) {
            Flash::error(__('application.collist_datas.not_found'));

            return redirect(route('application.collist_datas.index'));
        }
        return Admin::content(function (Content $content) use ($id, $collistData) {

            $content->header(__('application.collist_datas.edit_header'));
            $content->description(__('application.collist_datas.edit_description'));

            $content->body($this->form()->edit($id));
            $range = $collistData->range ? $collistData->range : 0;

            $content->row(view('application.collist-datas.edit', ['data_type' => $collistData->data_type, 'data_id' => $collistData->data_id, 'range' => $range, 'tpl_id' => $collistData->tpl_id]));
        });
    }

    /**
     * Update the specified CollistData in storage.
     *
     * @param  int              $id
     * @param CollistDataRequest $request
     *
     * @return Response
     */
    public function update($id, Request $request)
    {
        $collistData = $this->collistDataRepository->findWithoutFail($id);

        if (empty($collistData)) {
            Flash::error(__('application.collist_datas.not_found'));

            return redirect(route('application.collist_datas.index'));
        }
        $input = $request->all();
        if (array_key_exists('_file_del_', $input)) {
            $bgtype       = $request->post('bg');
            $imgtype      = $request->post('icon');
            $uploadConfig = config('custom.base.upload.collist_data');
            $driver       = $uploadConfig['driver'];
            $dir          = $uploadConfig['dir'];
            if ($bgtype === '_file_del_') {
                $imgUrl = $collistData->bg;
                if ($flag = Storage::disk($driver)->delete($imgUrl)) {
                    $collistData->bg = null;
                    $collistData->save();
                    return response()->json(['message' => '图片删除成功!', 'status' => $flag]);
                }
            } elseif ($imgtype === '_file_del_') {
                $imgUrl = $collistData->icon;
                if ($flag = Storage::disk($driver)->delete($imgUrl)) {
                    $collistData->icon = null;
                    $collistData->save();
                    return response()->json(['message' => '图片删除成功!', 'status' => $flag]);
                }
                return response()->json(['message' => '图片删除失败!', 'status' => $flag]);
            }
        }

        $sValidators = [
            'collist_id' => 'required',
            'data_type'  => 'required',
            'tpl_id'     => "required",
            'data_id'    => [
                'required',
                Rule::unique('collist_datas')
                    ->where(function ($query) use ($input) {
                        $query->where(['collist_id' => $input['collist_id'], 'data_type' => $input['data_type']])->whereNull('deleted_at');
                    }),
            ],
        ];

        $validators = [];

        // 动态判断校验
        foreach ($input as $key => $value) {
            if (isset($sValidators[$key])) {
                $validators[$key] = $sValidators[$key];
            }
        }

        // 定义校验, 没有移到request
        $validator = Validator::make($request->all(), $validators, [
            'required' => ':attribute 不能为空',
            'unique'   => ':attribute 已在此栏目列表中存在',
        ], [
            'data_type'  => '名称',
            'tpl_id'     => '模板',
            'data_id'    => '数据',
            'collist_id' => '栏目列表',

        ]);

        if ($validator) {
            // 校验
            if ($validator->fails()) {
                return back()->withErrors($validator)->withInput();
            }
        }

        $image = $request->file('icon'); //使用laravel 自带的request类来获取一下文件.
        if ($image) {
            if ($image->isValid()) {
                // 允许上传的图片格式
                $allowed_extensions = ["png", "jpg", "gif", "jpeg"];
                if ($image->getClientOriginalExtension() && !in_array($image->getClientOriginalExtension(), $allowed_extensions)) {
                    return ['error' => 'You may only upload png, jpg, jpeg or gif.'];
                }
                $realPath = $image->getRealPath(); //临时文件的绝对路径
                // 获得图片的扩展名
                $extension = $image->getClientOriginalExtension();
                // 获取上传图片的文件名
                // $imagesName = $image->getClientOriginalName();
                // 重新命名上传文件名字
                $newImageName = md5(time()) . random_int(5, 5) . "." . $extension;
                // 上传图片
                $uploadConfig = config('custom.base.upload.collist_data');
                $driver       = $uploadConfig['driver'];
                $dir          = $uploadConfig['dir'];
                $rootDir      = config('filesystems.disks.' . $driver . '.upload');
                // 图片的完整路径
                $fullPath = $rootDir . '/' . $dir . '/' . $newImageName;

                $input['icon'] = $fullPath;
                $flag          = Storage::disk($driver)->delete($collistData->icon);

                $bool = Storage::disk($driver)->put($fullPath, file_get_contents($realPath));
            }
        }
        // 背景图片
        $backgroundImage = $request->file('bg'); //使用laravel 自带的request类来获取一下文件.
        if ($backgroundImage) {
            if ($backgroundImage->isValid()) {
                // 允许上传的图片格式
                $allowed_extensions = ["png", "jpg", "gif", "jpeg"];
                if ($backgroundImage->getClientOriginalExtension() && !in_array($backgroundImage->getClientOriginalExtension(), $allowed_extensions)) {
                    return ['error' => 'You may only upload png, jpg, jpeg or gif.'];
                }
                $bgRealPath = $backgroundImage->getRealPath(); //临时文件的绝对路径
                // 获得图片的扩展名
                $bgExtension = $backgroundImage->getClientOriginalExtension();
                // 获取上传图片的文件名
                // $imagesName = $image->getClientOriginalName();
                // 重新命名上传文件名字
                $newBackgroundImageName = md5(time()) . random_int(5, 5) . "." . $bgExtension;
                // 上传图片
                $uploadConfig = config('custom.base.upload.collist_data');
                $driver       = $uploadConfig['driver'];
                $dir          = $uploadConfig['dir'];
                $rootDir      = config('filesystems.disks.' . $driver . '.upload');
                // 图片的完整路径
                $bgFullPath  = $rootDir . '/' . $dir . '/' . $newBackgroundImageName;
                $input['bg'] = $bgFullPath;

                $flag = Storage::disk($driver)->delete($collistData->bg);

                $bool = Storage::disk($driver)->put($bgFullPath, file_get_contents($bgRealPath));
            }
        }

        $collistData = $this->collistDataRepository->update($input, $id);
        if ($collistData) {
            // 保存当前栏目列表的标签
            if (is_array($request->get('tags'))) {
                // $collist->tags()->detach();
                $collistData->tags()->sync(array_filter($request->get('tags')));
            }
        }
        Flash::success(__('application.collist_datas.updated_success'));

        return Input::get('_previous_') ? redirect(Input::get('_previous_')) : redirect(route('application.collist_datas.index'));
    }

    /**
     * Remove the specified CollistData from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        // 根据 `,` 判断传递过来的是单个id还是多个id
        if (substr_count($id, ',') >= 1) {
            $id = explode(",", $id);
        }

        // 如果是数组则进行批量删除
        if (is_array($id)) {
            if ($flag = $this->collistDataRepository->batchDelete('id', $id)) {

                return response()->json(['message' => __('application.collist_datas.deleted_success'), 'status' => $flag]);
            } else {
                return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
            }
        }

        $collistData = $this->collistDataRepository->findWithoutFail($id);

        if (empty($collistData)) {
            Flash::error(__('application.collist_datas.not_found'));

            return redirect(route('application.collist_datas.index'));
        }

        if ($flag = $this->collistDataRepository->delete($id)) {

            $uploadConfig = config('custom.base.upload.collist_data');
            $driver       = $uploadConfig['driver'];
            $deleteImg    = Storage::disk($driver)->delete($collistData->bg);
            $deleteImg    = Storage::disk($driver)->delete($collistData->icon);
            $collistData->tags()->detach();

            return response()->json(['message' => __('application.collist_datas.deleted_success'), 'status' => $flag]);
        } else {
            return response()->json(['message' => __('base.deleted.error'), 'status' => $flag]);
        }
    }

    /**
     * [form description]
     * @return {[type]} [description]
     */
    protected function form()
    {
        return Admin::form(CollistData::class, function (Form $form) {

            $form->display('id', 'ID');
            $collists = Collist::pluck('title', 'id');
            $form->select('collist_id', '栏目列表')->options($collists);
            //编辑顶级有问题
            // $arrAreas = Area::visualRegion()->orderBy('listorder', 'asc')->get(['f_id', 'f_pid', 'alias'])->toArray();
            $tmpColumnTypes = ColumnType::get(['tags', 'id', 'title', 'pid'])->toArray();
            $tempArr        = [];
            foreach ($tmpColumnTypes as $key => $columnType) {
                $tags = explode(',', $columnType['tags']);
                foreach ($tags as $tag) {
                    if (in_array($tag, ['any', 'collist'])) {
                        $tempArr[] = $columnType['id'];
                    }
                }
            }
            $columnTypes = ColumnType::whereIn('id', $tempArr)->get(['id', 'pid', 'title'])->toArray();
            $columnTypes = array_merge([['id' => 0, 'pid' => 0, 'title' => '选择']], $columnTypes);
            $form->ztree('data_type', '栏目类型')
                ->zTreeConfig(["map" => ["id" => "id", "title" => 'name', 'pid' => 'pId'], "type" => "radio", "open" => true])
                ->options($columnTypes)->default('1')->load('data_id', route('application.collist_datas.datas.resources'), 'id', 'title');
            // $columnTypes = ColumnType::whereIn('id', $tempArr)->pluck('title', 'id');
            // $form->select('data_type', '数据类型')->options($columnTypes)->default('1')->load('data_id', route('application.collist_datas.datas.resources'), 'id', 'title');
            $muticolumns = Muticolumn::orderBy('listorder', 'asc')->pluck('title', 'id');
            // $muticolumns
            $form->select('data_id', '数据')->options();
            $form->select('range', 'range')->options(array(
                0 => '选择',
                1 => 'area',
                2 => 'page',
                3 => 'app',
                4 => 'list',
            ))->load('tpl_id', route('entity.column.collists.tpls'), 'id', 'title')->help('选中range时模板不能为空');
            $paneltpls = Paneltpl::pluck('title', 'id')->toArray();
            $form->select('tpl_id', '模板')->options(array_merge(['0' => '选择'], $paneltpls));
            // 背景图
            $form->image('bg', '背景图')->removable();
            // message
            $form->text('message', 'message');
            $tags = tag::pluck('name', 'id');
            $form->multipleSelect('tags', '标签')->options($tags);

            $form->image('icon', '栏目ICON')->removable();
            $form->textarea('data', 'data');
            $form->number('listorder', '排序');

            $form->display('created_at', __('base.created_at'));
            $form->display('updated_at', __('base.updated_at'));

        });
    }

    /**
     * [grid description]
     * @return {[type]} [description]
     */
    protected function grid()
    {

        return Admin::grid(CollistData::class, function (Grid $grid) {
            // 考虑是否需要scope和排序
            $grid->model()->orderBy('listorder', 'asc');

            // // 添加按钮
            // if (!\Gate::check('application.collist_datas.create')) {
            //     $grid->disableCreation();
            // }

            // // 编辑和删除按钮
            // $grid->actions(function ($actions) {
            //     // 编辑按钮
            //     if (!\Gate::check('application.collist_datas.edit')) {
            //         $actions->disableEdit();
            //     }
            //     // 删除按钮
            //     if (!\Gate::check('application.collist_datas.destroy')) {
            //         $actions->disableDelete();
            //     }
            // });

            // // 导出按钮
            // if (!\Gate::check('application.collist_datas.export')) {
            //     $grid->disableExport();
            // }

            // // 批量操作
            // $grid->tools(function ($tools) {
            //     $tools->batch(function ($batch) {
            //         // 批量删除按钮
            //         if (!\Gate::check('application.collist_datas.batch_destroy')) {
            //             $batch->disableDelete();
            //         }
            //     });
            // });

            // 添加按钮
            if (Admin::user()->cannot('application.collist_datas.create')) {
                $grid->disableCreation();
            }

            // 编辑和删除按钮
            $grid->actions(function ($actions) {
                // 编辑按钮
                if (Admin::user()->cannot('application.collist_datas.edit')) {
                    $actions->disableEdit();
                }
                // 删除按钮
                if (Admin::user()->cannot('application.collist_datas.destroy')) {
                    $actions->disableDelete();
                }
            });

            // 导出按钮
            if (Admin::user()->cannot('application.collist_datas.export')) {
                $grid->disableExport();
            }

            // 批量操作
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    // 批量删除按钮
                    if (Admin::user()->cannot('application.collist_datas.batch_destroy')) {
                        $batch->disableDelete();
                    }
                });
                $tools->append('<a href="javascript:void(0)" onclick="history.go(-1)" class="btn btn-default btn-sm" style="margin-right: 0px"> <i class="fa fa-arrow-left"></i>返回</a>');
            });

            $grid->column('id', 'ID')->sortable();
            $grid->column('collist.title', '栏目列表')->sortable();
            $grid->column('columnType.title', '数据类型');

            $grid->column('data_id', '数据')->display(function ($value) {
                $name       = '';
                $columnType = ColumnType::where(['id' => $this->data_type])->first();
                if ($columnType) {
                    switch ($columnType->name) {
                        case 'collist':
                            $name = Collist::where(['id' => $this->data_id])->value('title');
                            break;
                        case 'panellist':
                            $name = ApppageRectpl::where(['id' => $this->data_id])->value('title');
                            break;
                        case 'reclist0':
                            $name = Reclist0::where(['id' => $this->data_id])->value('title');
                            break;
                        case 'vodpkg':
                            $name = Vasseti::where(['id' => $this->data_id])->value('title');
                            break;
                        case 'livepkg':
                            $name = Vasseti::where(['id' => $this->data_id])->value('title');
                            break;
                        case 'column':
                            $name = Muticolumn::where(['id' => $this->data_id])->value('title');
                            break;
                        case 'assetpkg':
                            $name = Vasseti::where(['id' => $this->resource_data_id])->value('title');
                            break;
                        case 'SPFJ':
                            $name = Vasseti::where(['id' => $this->resource_data_id])->value('title');
                            break;
                        case 'SPDJ':
                            $name = Vasseti::where(['id' => $this->resource_data_id])->value('title');
                            break;
                        default:
                            # code...
                            break;
                    }
                }
                return empty($name) ? '' : $name;
            });
            $grid->column('range', 'range')->display(function ($val) {
                switch ($val) {
                    case '1':
                        return '<span class="badge bg-green">area</span>';
                        break;
                    case '2':
                        return '<span class="badge bg-green">page</span>';
                        break;
                    case '3':
                        return '<span class="badge bg-green">app</span>';
                        break;
                    case '4':
                        return '<span class="badge bg-green">list</span>';
                        break;
                    default:
                        return '';
                        break;
                }
            });
            $grid->column('tpl_id', '模板')->display(function ($val) {
                $returnName = '';
                switch ($this->range) {
                    case '1':
                        $returnName = Paneltpl::where(['id' => $val])->value('title');
                        break;
                    default:
                        $returnName = Pagetpl::where(['id' => $val])->value('title');
                        break;
                }
                return empty($returnName) ? '' : $returnName;
            });

            $uploadConfig = config('custom.base.upload.collist_data');
            $driver       = $uploadConfig['driver'];
            $dir          = $uploadConfig['dir'];
            $uploadHosts  = config('custom.base.host');
            $host         = $uploadHosts[$driver];
            // 背景图
            $grid->column('bg', '背景图')->image($host);
            // 信息
            $grid->column('message', 'message')->sortable();

            $grid->tags('标签')->pluck('name')->label();
            $grid->column('icon', '栏目ICON')->image($host);
            $grid->column('data', 'data')->sortable();
            $grid->column('listorder', '排序')->sortable();

            /**
             * 过滤处理
             */
            $grid->filter(function ($filter) {
                // // 禁用id查询框
                // $filter->disableIdFilter();
                $filter->like('name', '名称');

                // 栏目列表
                $tmpCollists = Collist::pluck('title', 'id');
                $filter->equal('collist_id', '栏目列表')->select($tmpCollists);

            });
        });
    }

    /**
     * 根据数据类型获取数据
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function getReasoureByDataType(Request $request)
    {
        $dataId     = $request->get('q');
        $returnData = [];
        $columnType = ColumnType::where(['id' => $dataId])->first();
        if ($columnType) {
            switch (trim($columnType->name)) {
                case 'collist':
                    $returnData = Collist::orderBy('listorder', 'asc')->get(['title', 'id'])->toArray();
                    break;
                case 'panellist':
                    $returnData = ApppageRectpl::get(['title', 'id'])->toArray();
                    break;
                case 'reclist0':
                    $returnData = Reclist0::get(['title', 'id'])->toArray();
                    break;
                case 'vodpkg':
                    $returnData = Vasseti::orderBy('listorder', 'asc')->get(['title', 'id'])->toArray();
                    break;
                case 'livepkg':
                    $returnData = Vasseti::orderBy('listorder', 'asc')->get(['title', 'id'])->toArray();
                    break;
                case 'column':
                    $returnData = Muticolumn::orderBy('listorder', 'asc')->get(['title', 'id'])->toArray();
                    break;
                case 'assetpkg':
                    $returnData = Vasseti::orderBy('listorder', 'asc')->get(['title', 'id'])->toArray();
                    break;
                case 'SPFJ':
                    $returnData = Vasseti::orderBy('listorder', 'asc')->where('serials', '>', 1)->get(['title', 'id'])->toArray();
                    break;
                case 'SPDJ':
                    $returnData = Vasseti::orderBy('listorder', 'asc')->where('serials', '=', 1)->get(['title', 'id'])->toArray();
                    break;
                default:
                    # code...
                    break;
            }
        }
        return array_merge([['id' => '0', 'title' => '选择']], $returnData);

    }

    /**
     * ajax根据数据类型获取数据
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function getReasourceData($dataId, Request $request)
    {
        $datas      = [];
        $columnType = ColumnType::where(['id' => $dataId])->first();
        if ($columnType) {
            switch (trim($columnType->name)) {
                case 'collist':
                    $datas = Collist::orderBy('listorder', 'asc')->get(['title', 'id'])->toArray();
                    break;
                case 'panellist':
                    $datas = ApppageRectpl::get(['title', 'id'])->toArray();
                    break;
                case 'reclist0':
                    $datas = Reclist0::get(['title', 'id'])->toArray();
                    break;
                case 'vodpkg':
                    $datas = Vasseti::orderBy('listorder', 'asc')->get(['title', 'id'])->toArray();
                    break;
                case 'livepkg':
                    $datas = Vasseti::orderBy('listorder', 'asc')->get(['title', 'id'])->toArray();
                    break;
                case 'column':
                    $datas = Muticolumn::orderBy('listorder', 'asc')->get(['title', 'id'])->toArray();
                    break;
                case 'assetpkg':
                    $datas = Vasseti::orderBy('listorder', 'asc')->get(['title', 'id'])->toArray();
                    break;
                case 'SPFJ':
                    $datas = Vasseti::orderBy('listorder', 'asc')->where('serials', '>', 1)->get(['title', 'id'])->toArray();
                    break;
                case 'SPDJ':
                    $datas = Vasseti::orderBy('listorder', 'asc')->where('serials', '=', 1)->get(['title', 'id'])->toArray();
                    break;
                default:
                    # code...
                    break;
            }
        }
        return response()->json(['content' => array_merge([['id' => '0', 'title' => '选择']], $datas), 'status' => true]);
    }
}
